﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class YFKLR_YFKLRAdd : System.Web.UI.Page
{
  string sID = "";
  protected void Page_Load(object sender, EventArgs e)
  {
    if (Request.QueryString["id"] != null)
    {
      sID = Request.QueryString["id"].ToString();
    }
    if (!Page.IsPostBack)
    {
      if (!string.IsNullOrEmpty(sID))
        InitData();
      //DDLManage.BindKH(ddlKH);
    }
  }

  protected void InitData()
  {
    string sSql = "select *,convert(varchar,yfklrrq,23) rq from sjlr_yfklr where yfkid=@0";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = sID;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    if (dr.HasRows)
    {
      dr.Read();
      txtName.Text = dr["yfk"].ToString();
      dateJG.Text = dr["rq"].ToString();
      txtBZ.Text = dr["yfkbz"].ToString();
      ddlKH.SelectedValue = dr["khid"].ToString();
      //dateJG.Enabled = false;
    }
    dr.Close();
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(sID))
      Insert();
    else
      Update();
  }

  //插入
  protected void Insert()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在押金";
    //  return;
    //}
    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;
    string sID = GetID();

    sSql = "insert into sjlr_yfklr values(@0,@1,@2,@3,@4,@4)";
    cmdParams = new SqlParameter[6] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12),
     new SqlParameter("@2", SqlDbType.VarChar, 12),new SqlParameter("@3", SqlDbType.VarChar, 20),
     new SqlParameter("@4", SqlDbType.VarChar, 20),new SqlParameter("@5", SqlDbType.VarChar, 200)};
    cmdParams[0].Value = sID;
    cmdParams[1].Value = SysManage.Get_User;
    cmdParams[2].Value = ddlKH.SelectedValue;
    cmdParams[3].Value = dateJG.Text;
    cmdParams[4].Value = txtName.Text;
    cmdParams[5].Value = txtBZ.Text;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
    }
  }

  //判断此名称是否存在
  protected bool ExistID()
  {
    bool flag = false;
    string sSql = "select yfkid as num from sjlr_yfklr where yfklrrq>=@0";
    SqlParameter[] cmdParams = { new SqlParameter("@0", SqlDbType.VarChar, 20) };
    cmdParams[0].Value = dateJG.Text;
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql, cmdParams);
    if (dr.HasRows)
    {
      flag = true;
    }
    dr.Close();
    return flag;
  }

  protected string GetID()
  {
    string sID = "1";
    string sSql = "select isnull(max(yfkid)+1,'1') id from sjlr_yfklr";
    SqlDataReader dr = DbHelperSQL.ExecuteReader(sSql);
    if (dr.HasRows)
    {
      dr.Read();
      sID = dr["id"].ToString();
    }
    dr.Close();
    return sID;
  }

  //修改
  protected void Update()
  {
    //if (ExistID())
    //{
    //  lbMessage.Text = "此日期已存在押金";
    //  return;
    //}
    SqlParameter[] cmdParams = null;
    string sSql = "";
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci;

    sSql = "delete from sjlr_yfklr where yfkid=@0";
    cmdParams = new SqlParameter[1] { new SqlParameter("@0", SqlDbType.VarChar, 10) };
    cmdParams[0].Value = sID;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    sSql = "insert into sjlr_yfklr values(@0,@1,@2,@3,@4,@4)";
    cmdParams = new SqlParameter[6] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12),
     new SqlParameter("@2", SqlDbType.VarChar, 12),new SqlParameter("@3", SqlDbType.VarChar, 20),
     new SqlParameter("@4", SqlDbType.VarChar, 20),new SqlParameter("@5", SqlDbType.VarChar, 200)};
    cmdParams[0].Value = sID;
    cmdParams[1].Value = SysManage.Get_User;
    cmdParams[2].Value = ddlKH.SelectedValue;
    cmdParams[3].Value = dateJG.Text;
    cmdParams[4].Value = txtName.Text;
    cmdParams[5].Value = txtBZ.Text;
    ci = new CommandInfo();
    ci.CommandText = sSql;
    ci.Parameters = cmdParams;
    cis.Add(ci);

    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
    }
  }
}
